<template>
  <div class="stats-panel">
    <div class="stats-grid">
      <!-- 总数统计 -->
      <div class="stat-card main-stat">
        <div class="stat-icon">🏛️</div>
        <div class="stat-label">逝去的网站</div>
        <div class="stat-sublabel">记录在案的数字遗迹</div>
        <div class="stat-content">
          <div class="stat-number">
            <AnimatedNumber :value="stats.total" />
          </div>
        </div>
      </div>

      <!-- 筛选结果统计 -->
      <div class="stat-card" v-if="stats.filtered !== stats.total">
        <div class="stat-icon">🔍</div>
        <div class="stat-content">
          <div class="stat-number">
            <AnimatedNumber :value="stats.filtered" />
          </div>
          <div class="stat-label">筛选结果</div>
          <div class="stat-sublabel">符合条件的网站</div>
        </div>
      </div>

      <!-- 地区分布 -->
      <div class="stat-card row-card">
        <div class="stat-icon">🌍</div>
        <div class="stat-label">地区分布</div>
        <div class="stat-content">
          <div class="stat-breakdown">
            <div class="breakdown-item">
              <span class="breakdown-label">国内</span>
              <span class="breakdown-value">
                <AnimatedNumber :value="stats.domestic || 0" />
              </span>
            </div>
            <div class="breakdown-item">
              <span class="breakdown-label">国外</span>
              <span class="breakdown-value">
                <AnimatedNumber :value="stats.international || 0" />
              </span>
            </div>
          </div>
        </div>
      </div>

      <!-- 类型分布 -->
      <div class="stat-card row-card">
        <div class="stat-icon">📂</div>
        <div class="stat-label">主要类型</div>
        <div class="stat-content">
          <div class="stat-breakdown">
            <template v-if="Object.keys(topTypes).length > 0">
              <div
                v-for="(count, type) in topTypes"
                :key="type"
                class="breakdown-item"
              >
                <span class="breakdown-label">{{ getTypeLabel(type) }}</span>
                <span class="breakdown-value">
                  <AnimatedNumber :value="count" />
                </span>
              </div>
            </template>
            <template v-else>
              <div class="breakdown-item">
                <span class="breakdown-label">暂无数据</span>
                <span class="breakdown-value">-</span>
              </div>
            </template>
          </div>
        </div>
      </div>

      <!-- 年份统计 -->
      <div class="stat-card row-card">
        <div class="stat-icon">📅</div>
        <div class="stat-label">时间跨度</div>
        <div class="stat-content">
          <div class="stat-breakdown">
            <div class="breakdown-item">
              <span class="breakdown-label">最早</span>
              <span class="breakdown-value">{{ minYear || "-" }}</span>
            </div>
            <div class="breakdown-item">
              <span class="breakdown-label">最近</span>
              <span class="breakdown-value">{{ maxYear || "-" }}</span>
            </div>
          </div>
        </div>
      </div>

      <!-- 热门分类 -->
      <div class="stat-card wide-card">
        <div class="stat-icon">🏷️</div>
        <div class="stat-label">热门分类</div>
        <div class="stat-content">
          <div class="category-tags">
            <template v-if="Object.keys(topCategories).length > 0">
              <div
                v-for="(count, category) in topCategories"
                :key="category"
                class="category-tag"
                :style="{ '--tag-size': 0.8 }"
              >
                <span class="category-name">{{ category }}</span>
                <span class="category-count">{{ count }}</span>
              </div>
            </template>
            <template v-else>
              <div class="category-tag">
                <span class="category-name">暂无数据</span>
                <span class="category-count">-</span>
              </div>
            </template>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed } from "vue";
import { useCemeteryStore } from "@/stores/cemetery";
import AnimatedNumber from "./AnimatedNumber.vue";

const props = defineProps({
  showTrend: {
    type: Boolean,
    default: true,
  },
  maxCategories: {
    type: Number,
    default: 8,
  },
  maxTypes: {
    type: Number,
    default: 4,
  },
});

const store = useCemeteryStore();

// 计算属性
const stats = computed(() => {
  const storeStats = store.stats;
  return {
    total: storeStats?.total || 0,
    filtered: storeStats?.filtered || 0,
    domestic: storeStats?.domestic || 0,
    international: storeStats?.international || 0,
    byType: storeStats?.byType || {},
    byCategory: storeStats?.byCategory || {},
    byYear: storeStats?.byYear || {},
  };
});

const topTypes = computed(() => {
  const byType = stats.value.byType || {};
  const types = Object.entries(byType)
    .sort(([, a], [, b]) => b - a)
    .slice(0, props.maxTypes);
  return Object.fromEntries(types);
});

const topCategories = computed(() => {
  const byCategory = stats.value.byCategory || {};
  const categories = Object.entries(byCategory)
    .sort(([, a], [, b]) => b - a)
    .slice(0, props.maxCategories);
  return Object.fromEntries(categories);
});

const minYear = computed(() => {
  const byYear = stats.value.byYear || {};
  const years = Object.keys(byYear).map(Number).filter(Boolean);
  return years.length > 0 ? Math.min(...years) : null;
});

const maxYear = computed(() => {
  const byYear = stats.value.byYear || {};
  const years = Object.keys(byYear).map(Number).filter(Boolean);
  return years.length > 0 ? Math.max(...years) : null;
});

// 方法
const getTypeLabel = (type) => {
  const labels = {
    website: "网站",
    app: "应用",
    service: "服务",
    platform: "平台",
    game: "游戏",
  };
  return labels[type] || type;
};

</script>

<style scoped>
.stats-panel {
  margin-top: 2rem;
  width: 100%;
}

.stats-grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 1.5rem;
  margin-bottom: 2rem;
}

.stat-card {
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--border-color);
  border-radius: 12px;
  padding: 1.5rem;
  backdrop-filter: blur(10px);
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.stat-card::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: linear-gradient(
    90deg,
    var(--accent-color),
    var(--secondary-color)
  );
  opacity: 0;
  transition: opacity 0.3s ease;
}

.stat-card:hover {
  background: rgba(255, 255, 255, 0.08);
  border-color: var(--accent-color);
  transform: translateY(-2px);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
}

.stat-card:hover::before {
  opacity: 1;
}

.main-stat {
  grid-column: 1 / -1;
  background: linear-gradient(
    135deg,
    rgba(255, 255, 255, 0.1),
    rgba(255, 255, 255, 0.05)
  );
}

.wide-card {
  grid-column: 1 / -1;
}


.stat-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
  text-align: center;
}

.stat-icon {
  font-size: 2.5rem;
  opacity: 0.8;
  flex-shrink: 0;
}

.main-stat .stat-icon {
  font-size: 3rem;
}

.stat-content {
  flex: 1;
  min-width: 0;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.stat-number {
  font-size: 2.5rem;
  font-weight: 700;
  color: var(--accent-color);
  line-height: 1;
  margin-bottom: 0.5rem;
}

.main-stat .stat-number {
  font-size: 3.5rem;
  background: linear-gradient(
    135deg,
    var(--accent-color),
    var(--secondary-color)
  );
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.stat-label {
  font-size: 1.1rem;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 0.25rem;
}

.stat-sublabel {
  font-size: 0.9rem;
  color: var(--text-secondary);
  opacity: 0.8;
}

/* 分解统计样式 */
.stat-breakdown {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  margin-bottom: 1rem;
  width: 100%;
}

.breakdown-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.5rem 0;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.breakdown-item:last-child {
  border-bottom: none;
}

.breakdown-label {
  color: var(--text-secondary);
  font-size: 0.9rem;
}

.breakdown-value {
  color: var(--text-primary);
  font-weight: 600;
  font-size: 1.1rem;
}

/* 分类标签样式 */
.category-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  margin-bottom: 1rem;
  justify-content: center;
  width: 100%;
}

.category-tag {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid var(--border-color);
  border-radius: 20px;
  padding: 0.5rem 1rem;
  font-size: calc(0.8rem * var(--tag-size, 1));
  transform: scale(var(--tag-size, 1));
  transition: all 0.2s ease;
}

.category-tag:hover {
  background: rgba(255, 255, 255, 0.15);
  border-color: var(--accent-color);
}

.category-name {
  color: var(--text-primary);
  font-weight: 500;
}

.category-count {
  background: var(--accent-color);
  color: white;
  font-size: 0.8rem;
  font-weight: 600;
  padding: 0.2rem 0.5rem;
  border-radius: 10px;
  min-width: 20px;
  text-align: center;
}


/* 响应式设计 */
@media (max-width: 1024px) {
  .stats-grid {
    grid-template-columns: 1fr 1fr;
  }

  .main-stat,
  .wide-card {
    grid-column: 1 / -1;
  }

  .row-card:nth-of-type(3) {
    grid-column: 1 / -1;
  }
}

@media (max-width: 768px) {
  .stats-grid {
    grid-template-columns: 1fr;
    gap: 1rem;
  }

  .main-stat,
  .wide-card,
  .row-card {
    grid-column: 1 / -1;
  }

  .stat-card {
    padding: 1rem;
  }

  .stat-number {
    font-size: 2rem;
  }

  .main-stat .stat-number {
    font-size: 2.5rem;
  }

  .stat-icon {
    font-size: 2rem;
  }

  .main-stat .stat-icon {
    font-size: 2.5rem;
  }

  .category-tags {
    gap: 0.5rem;
  }

}

@media (max-width: 480px) {
  .breakdown-item {
    padding: 0.4rem 0;
  }

  .category-tag {
    font-size: 0.8rem;
    padding: 0.4rem 0.8rem;
  }
}
</style>
